from selenium.webdriver.common.by import By

from base.mp.base import BasePage, BaseHandle
from utils import UtilsDriver, choice_channel


class PublishPage(BasePage):
    def __init__(self):
        super().__init__()
        self.title=By.XPATH,'//*[@id="app"]/div/div[2]/section/div/form/div[1]/div/div/input'
        self.iframe_ele=By.ID,"publishTinymce_ifr"
        self.content=By.XPATH,'//*[@id="tinymce"]'
        self.cover=By.XPATH,'//*[@id="app"]/div/div[2]/section/div/form/div[3]/div/div/label[3]/span[1]/span'
        self.channel=By.XPATH,'//*[@id="app"]/div/div[2]/section/div/form/div[4]/div/div/div[1]/input'
        self.publish_btn=By.XPATH,'//*[@id="app"]/div/div[2]/section/div/form/div[5]/div/button[1]'
    def find_title(self):
        return self.get_element(self.title)
    def find_iframe(self):
        return self.get_element(self.iframe_ele)
    def find_input_kw(self):
        return self.get_element(self.content)
    def find_cover(self):
        return self.get_element(self.cover)
    def find_channel_choice(self):
        return self.get_element(self.channel)
    def find_publish_btn(self):
        return self.get_element(self.publish_btn)
class PublicHandle(BaseHandle):
    def __init__(self):
        self.publish_page=PublishPage()
        self.driver=UtilsDriver.get_mp_driver()
    def input_title(self,title):
        self.input_text(self.publish_page.find_title(),title)
    def input_content(self,content):
        self.driver.switch_to.frame(self.publish_page.find_iframe())
        self.input_text(self.publish_page.find_input_kw(),content)
        self.driver.switch_to.default_content()
    def choice_cover(self):
        self.publish_page.find_cover().click()
    def choice_channel(self,channel):
        choice_channel(self.driver,self.publish_page.find_channel_choice(),channel)
    def click_publish_btn(self):
        self.publish_page.find_publish_btn().click()
class PublishProxy:
    def __init__(self):
        self.publish_handle=PublicHandle()
    def publish_article(self,title,content,channel):
        self.publish_handle.input_title(title)
        self.publish_handle.input_content(content)
        self.publish_handle.choice_cover()
        self.publish_handle.choice_channel(channel)
        self.publish_handle.click_publish_btn()